import os
import sys
import pkg_resources

ENTRYPOINT='myplayer.plugins'
PLUGIN_DIR=os.path.join(os.path.dirname(__file__),'plugins')

def init_plugins():
    import pdb;pdb.set_trace()
    pkg_resources.working_set.add_entry(PLUGIN_DIR)
    pkg_env=pkg_resources.Environment([PLUGIN_DIR])
    plugins={}
    for name in pkg_env:
	egg=pkg_env[name][0]
	egg.activate()
	modules=[]
	for name in egg.get_entry_map(ENTRYPOINT):
	    entry_point=egg.get_entry_info(ENTRYPOINT,name)
	    cls=entry_point.load()
	    if not hasattr(cls,'capabilities'):
		cls.capabilities=[]
	    instance=cls()
	    for c in cls.capabilities:
		plugins.setdefault(c,[]).append(instance)
    return plugins
    
plugins=init_plugins()

if __name__=='__main__':
    print plugins
	    
